#include "dirmodel.h"
#include <QDir>
#include <QIcon>
#include <QDateTime>
#include <QDebug>
#include <QInternal>
#include <QFileIconProvider>
#include <QCheckBox>

DirModel::DirModel(QObject *parent)
    : QAbstractTableModel(parent)
{
}

QVariant DirModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(role!=Qt::DisplayRole)
        return QVariant();

    if(orientation==Qt::Horizontal)
        if(section==0) return QVariant();
        else if(section==1) return tr("文件名");
        else if(section==2) return tr("后缀");
        else if(section==3) return tr("大小");
        else if(section==4) return tr("创建时间");
        else if(section==5) return tr("修改时间");
        else  return QString("%1").arg(section);
    else
         return QString("%1").arg(section+1);

}

int DirModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;

    return filelist.length();
    // FIXME: Implement me!
}

int DirModel::columnCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;

    return 6;
    // FIXME: Implement me!
}

QVariant DirModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();
    QFileInfo f = filelist.at(index.row());
    int col = index.column();
    if(role==Qt::DisplayRole){
        switch (col) {
        case 0:
        {
            QFileIconProvider *provider= new QFileIconProvider();
            QIcon icon = provider->icon(f);
            return icon;
            //return QCheckBox();
        }
        case 1:
            return (f.absoluteFilePath()).replace(dirPath+'/',QString());
        case 2:
            return f.suffix();
        case 3:
        {
            long long  FileSize1    =  f.size();
            if(FileSize1<1024){ return QString("%1B").arg(QString::number(FileSize1,'f',2));}
            else if(FileSize1<1024*1024) {return QString("%1KB").arg(QString::number(FileSize1/1024.0,'f',2));}
            else if(FileSize1<1024*1024*1024) {return QString("%1MB").arg(QString::number(FileSize1/1024/1024.0,'f',2));}
            else return FileSize1;
        }
        case 4:
            return f.birthTime();
        case 5:
            return f.lastModified();
        }
    }
    else if(role==Qt::DecorationRole&&col==1)//图标
    {
        QIcon icon = provider.icon(f);
        return icon;
    }
    else if(role==Qt::TextAlignmentRole) //对齐方式
    {
        return QVariant(Qt::AlignLeft|Qt::AlignVCenter);

    }else if(role==Qt::CheckStateRole)
    {
        if(col==0)
        {
            if (check_state_map.contains(index.row()))
               return check_state_map[index.row()] == Qt::Checked ? Qt::Checked : Qt::Unchecked;
            else
              return Qt::Unchecked;
        }
    }

    return QVariant();
}

bool DirModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if(!index.isValid())
        return false;

    if (role == Qt::CheckStateRole && index.column() == 0)
    {
        check_state_map[index.row()] = (value == Qt::Checked ? Qt::Checked : Qt::Unchecked);
    }

    return true;
}

Qt::ItemFlags DirModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return 0;

    if (index.column() == 0)
        return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;

    return  Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

void DirModel::SetRootDir(QString dir)
{

    QDir rootDir = QDir(dir);
    if(rootDir.exists())
    {
        filelist.clear();
        dirPath = dir;
        ReadFileInfo(rootDir);
        for(int i=0;i<filelist.count();i++)
        {
            check_state_map[i]=Qt::Checked;
        }
    };
}

QFileInfoList DirModel::getSelectFiles()
{
    QFileInfoList selectFiles;
    for(int i=0;i<filelist.count();i++)
    {
        if(check_state_map[i]==Qt::Checked){
            selectFiles.append(filelist[i]);
        }
    }
    return selectFiles;
}

void DirModel::ReadFileInfo(QDir dir)
{

    QFileInfoList list = dir.entryInfoList();
    foreach ( QFileInfo var, list) {

        if(var.isDir()==true)
        {
           if(var.fileName()=="." || var.fileName() == "..")continue;
            //qDebug()<<var;
            ReadFileInfo(QDir(var.absoluteFilePath()));
        }else {
            filelist.append(var);
            //qDebug()<<var;
        }
    }
}



